/** 
  统计分析
*/
<template>
    <div>
        <div>
            <condition-card
                @reset="reset"
                @search="onSubmit"
                label-position="right"
                label-width="150px"
            >
                <el-row :gutter="24" style="height: 50px;">
                    <el-col :span="8">
                        <el-form-item label="项目级别">
                            <el-select
                                v-model.trim="queryForm.condition.projectLevel"
                                clearable
                                filterable
                                style="width: 100%"
                                placeholder
                            >
                                <el-option
                                    v-for="item in options.projectLevel"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="项目名称">
                            <el-input
                                v-model.trim="queryForm.condition.projectName"
                                maxlength="32"
                                clearable
                                placeholder=""
                                style="width: 100%"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="国家/地区">
                            <el-select
                                v-model.trim="queryForm.condition.nationCode"
                                clearable
                                filterable
                                @change="getCollege"
                                style="width: 100%"
                                placeholder
                            >
                                <el-option
                                    v-for="item in options.country"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="24" style="height: 50px;">
                    <el-col :span="8">
                        <el-form-item label="合作大学">
                            <el-select
                                v-model.trim="queryForm.condition.collegeId"
                                clearable
                                filterable
                                style="width: 100%"
                                placeholder
                            >
                                <el-option
                                    v-for="item in options.collegeCn"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目申请学院">
                            <el-select
                                v-model.trim="queryForm.condition.facultyCode"
                                clearable
                                filterable
                                style="width: 100%"
                                placeholder
                            >
                                <el-option
                                    v-for="item in options.facultyList"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="培养层次">
                            <el-select
                                v-model.trim="queryForm.condition.trainings"
                                clearable
                                filterable
                                multiple
                                style="width: 100%"
                                placeholder
                            >
                                <el-option
                                    v-for="item in options.teachLevel"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <!-- 搜索框结束 -->

                <!-- 列表页开始 -->
            </condition-card>
            <el-card shadow="never" v-loading="loading" body-style="padding: 24px;">
                <h1 class="condition-title">校际交流项目统计分析</h1>
                <div class="sub-title">
                    <div class="float-right" style="display: flex;">
                        <el-button @click="exportAnalysis()">导出</el-button>
                    </div>
                </div>
                <edu-table
                    :pagination="false"
                    :order="true"
                    :tooltipEffect="tooltipEffect"
                    stripe
                    :data="dataList"
                    size="mini"
                    :pageNum="queryForm.pageNum_"
                    @selection-change="handleSelectionChange"
                >
                    <edu-table-column
                        label="项目级别"
                        prop="projectLevel"
                        align="left"
                    ></edu-table-column>
                    <edu-table-column
                        label="申请学院"
                        prop="applyFacultyName"
                        align="left"
                    ></edu-table-column>
                    <edu-table-column
                        label="培养层次"
                        prop="trainingLevelName"
                        align="left"
                    ></edu-table-column>
                    <edu-table-column
                        label="项目名称"
                        prop="projectName"
                        align="left"
                    ></edu-table-column>
                    <edu-table-column
                        label="项目负责人"
                        prop="projectHeadName"
                        width="120"
                        align="left"
                    ></edu-table-column>
                    <edu-table-column
                        label="国家/地区"
                        prop="projectNationName"
                        align="left"
                    ></edu-table-column>
                    <edu-table-column
                        label="合作大学"
                        prop="projectCollegeCnName"
                        align="left"
                    ></edu-table-column>
                    <edu-table-column
                        label="合作大学英文名称"
                        prop="projectCollegeEnName"
                        align="left"
                        width="160"
                    ></edu-table-column>
                    <edu-table-column
                        label="项目开始时间"
                        prop="projectStartTime"
                        width="140"
                        align="left"
                    ></edu-table-column>
                    <edu-table-column
                        label="项目结束时间"
                        prop="projectEndTime"
                        width="140"
                        align="left"
                    ></edu-table-column>
                    <edu-table-column
                        label="每年的中方留学生名额"
                        prop="projectInlandNumber"
                        width="160"
                        align="left"
                    ></edu-table-column>
                    <edu-table-column
                        label="每年的实际中方留学生名额"
                        prop="projectRealInlandNumber"
                        width="200"
                        align="left"
                    ></edu-table-column>
                    <edu-table-column
                        label="每年的外方留学生名额"
                        prop="projectOutlandNumber"
                        width="160"
                        align="left"
                    ></edu-table-column>
                    <edu-table-column
                        label="每年的实际外方留学生名额"
                        prop="projectRealOutlandNumber"
                        width="200"
                        align="left"
                    >
                        <template slot-scope="scope">
                            <el-input
                                v-model.trim="scope.row.projectRealOutlandNumber"
                                maxlength="32"
                                clearable
                                style="width: 100%"
                                v-show="!scope.row.ifEdit"
                            ></el-input>
                            <span v-show="scope.row.ifEdit">
                                {{ scope.row.projectRealOutlandNumber }}
                            </span>
                        </template>
                    </edu-table-column>
                    <!-- 操作 -->
                    <edu-table-column :label="$t('base.operate')" fit width="120" align="left">
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                v-show="!scope.row.ifEdit"
                                @click="saveNumber(scope.row)"
                            >
                                保存
                            </el-button>
                            <el-button
                                type="text"
                                v-show="scope.row.ifEdit"
                                @click="editNumber(scope.row)"
                            >
                                编辑
                            </el-button>
                            <el-button
                                type="text"
                                v-show="!scope.row.ifEdit"
                                @click="cancelNumber(scope.row)"
                            >
                                取消
                            </el-button>
                        </template>
                    </edu-table-column>
                </edu-table>
                <el-row type="flex" class="row-bg" justify="end">
                    <el-col>
                        <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="queryForm.pageNum_"
                            :page-sizes="[20, 40, 80, 100]"
                            :page-size="queryForm.pageSize_"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="queryForm.total_"
                            style="float:right;margin-top:10px"
                        ></el-pagination>
                    </el-col>
                </el-row>
            </el-card>
        </div>
        <edu-export-dialog :title="$t('message.prompt')" ref="downloadRef"></edu-export-dialog>
    </div>
</template>
<script>
import { queryDic } from 'common/src/api/dictionary'
import UTILS from 'common/src/utils/utils'
import InterSchoolComAppApi from 'common/src/api/interSchoolExchange/InterSchoolComApp'
import statisticalApi from 'common/src/api/interSchoolExchange/statisticalAnalysis'
export default {
    data() {
        return {
            queryForm: {
                pageNum_: 1,
                pageSize_: 20,
                total_: 0,
                condition: {
                    projectLevel: null,
                    projectName: '',
                    nationCode: '',
                    collegeId: '',
                    facultyCode: '',
                    trainings: []
                }
            },
            dataList: [],
            multipleSelection: {
                ids: []
            },
            options: {
                projectLevel: [{ value: 1, label: '院级' }, { value: 2, label: '校级' }],
                facultyList: [],
                country: [],
                teachLevel: [],
                collegeCn: [],
                teachLevel: []
            }
        }
    },

    created() {
        // 初始化列表页
        this.getListData()
        this.getCountry()
        this.getTeacherLevel()
        this.initOptions()
    },
    methods: {
        // 校验只能输入正整数
        // checkNubmer(e) {
        //     let flag = new RegExp('^[1-9][0-9]*$').test(e.target.value)
        //     if (!flag) {
        //         this.$message.error('请输入正整数')
        //         e.target.value = ''
        //     }
        // },
        // 字典获取 国家
        async getCountry() {
            // 筛选条件字典查询
            let data = {
                lang: this.$store.getters.language || 'cn',
                type: 'allChild',
                keys: [
                    'G_GJQU' // 国家/地区
                ]
            }
            await queryDic(data).then(res => {
                UTILS.fillSelect(
                    {
                        country: 'G_GJQU' // 国家/地区
                    },
                    res.data,
                    this.options
                )
            })
        },

        async initOptions() {
            // 筛选条件字典查询
            let data = {
                lang: this.$store.getters.language || 'cn',
                type: 'allChild',
                keys: [
                    'X_YX' // 学院
                ]
            }
            await queryDic(data).then(res => {
                UTILS.fillSelect(
                    {
                        facultyList: 'X_YX' // 学院
                    },
                    res.data,
                    this.options
                )
            })
        },

        getCollege(val) {
            let parm = {
                collegeId: null,
                code: val
            }

            this.queryForm.condition.collegeId = ''
            this.options.collegeCn = []
            if (val) {
                InterSchoolComAppApi.getCollegeListByNationCode(parm).then(res => {
                    res.data.forEach(col => {
                        this.options.collegeCn.push({
                            value: col.id,
                            label: col.collegeName
                        })
                    })
                })
            } else {
                this.options.collegeCn = []
            }
        },

        // 获取培养层次
        async getTeacherLevel() {
            InterSchoolComAppApi.queryTrainings().then(res => {
                res.data.forEach(level => {
                    this.options.teachLevel.push({
                        label: level.trainingName,
                        value: level.trainingCode
                    })
                })
            })
        },

        // 导出目录
        exportAnalysis() {
            let _this = this
            let parm = {
                projectLevel: this.queryForm.condition.projectLevel,
                projectName: this.queryForm.condition.projectName,
                nationCode: this.queryForm.condition.nationCode,
                collegeId: this.queryForm.condition.collegeId,
                facultyCode: this.queryForm.condition.facultyCode,
                trainings: this.queryForm.condition.trainings,
                projectIdList: this.multipleSelection.ids
            }
            if (this.dataList.length === 0) {
                this.$message.error('无数据')
            } else {
                this.$refs.downloadRef.open()
                statisticalApi.exportList(parm).then(res => {
                    if (res.data && res.data.key) {
                        this.exportNext(res.data.key)
                    }
                })
            }
        },
        exportNext(key) {
            setTimeout(() => {
                statisticalApi.exportExcelS(key).then(resS => {
                    if (resS.data && resS.data.path) {
                        statisticalApi.exportExcelT(resS.data.path).then(resT => {
                            this.$message({
                                message: this.$t('base.exportSuccess'),
                                type: 'success'
                            })
                            this.$refs.downloadRef.close()
                        })
                    } else {
                        this.exportNext(key)
                    }
                })
            }, 3000)
        },
        // 修改每年实际的外方留学生名额
        saveNumber(row) {
            let parm = {
                projectId: row.projectId,
                projectRealOutlandNumber: row.projectRealOutlandNumber
            }
            statisticalApi.editRealOutlandNumber(parm).then(res => {
                if (res.code === 200) {
                    this.$message({
                        message: '保存成功',
                        type: 'success'
                    })
                    this.getListData()
                    row.ifEdit = true
                }
            })
        },
        editNumber(row) {
            row.ifEdit = false
        },
        cancelNumber(row) {
            this.$confirm('您确定要取消修改吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                row.ifEdit = true
                this.getListData()
            })
        },
        // 重置筛选框
        reset() {
            this.queryForm = {
                pageNum_: 1,
                pageSize_: 20,
                total_: 0,
                condition: {
                    projectLevel: null,
                    projectName: '',
                    nationCode: '',
                    collegeId: '',
                    facultyCode: '',
                    trainings: []
                }
            }
            this.getListData()
        },

        // 查询按钮
        onSubmit() {
            this.queryForm.pageNum_ = 1
            this.queryForm.total_ = 0
            this.queryForm.pageSize_ = 20
            this.getListData()
        },

        // 获取列表
        getListData() {
            let _this = this
            this.dataList = []
            this.multipleSelection.ids = []
            statisticalApi.listAnalysis(this.queryForm).then(res => {
                res.data.list.forEach(revList => {
                    revList.ifEdit = true
                    _this.dataList.push(revList)
                    _this.queryForm.pageSize_ = res.data.pageSize_
                    _this.queryForm.total_ = res.data.total_
                    _this.queryForm.pageNum_ = res.data.pageNum_
                })
            })
        },
        handleSelectionChange(val) {
            this.multipleSelection.ids = []
            val.forEach(menu => {
                this.multipleSelection.ids.push(menu.projectId)
            })
        },
        handleSizeChange(val) {
            this.queryForm.pageSize_ = val
            this.getListData()
        },
        handleCurrentChange(val) {
            this.queryForm.pageNum_ = val
            this.getListData()
        }
    }
}
</script>
<style lang="scss" scoped>
.condition-title {
    color: #2b3b4e;
    font-size: 18px;
    line-height: 24px;
    font-weight: bold;
}
.sub-title {
    margin: 16px 0 65px 0;
    font-size: 16px;
    color: #000020;
    letter-spacing: 0;
}
</style>
